package org.example.videotran.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.example.videotran.pojo.LanguageChoose;
import org.example.videotran.pojo.ResponseMessage;
import org.example.videotran.pojo.TransVideo;
import org.example.videotran.pojo.User;
import org.example.videotran.pojo.dto.UserDto;
import org.example.videotran.service.IVideoService;
import org.example.videotran.utils.FileUtils;
import org.example.videotran.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

@RestController //接口方法返回对象转换为json文本
@RequestMapping("/api/video") //localhost:8080/api/video/***
public class VideoController {

    @Autowired
    IVideoService iVideoService;

    @PostMapping("/recogn")
    public ResponseMessage<User> recogn(@RequestParam("video") MultipartFile video, @RequestParam("target_language") String target_language,@RequestParam("source_code") String source_code,@RequestParam("split_type") String split_type) throws JsonProcessingException {
        if (!video.getOriginalFilename().endsWith(".mp4") || video.isEmpty()) {
            return ResponseMessage.fail("文件类型错误或者为空");
        }
        LanguageChoose languageChoose = new LanguageChoose(target_language,source_code,split_type);

        ResponseEntity<String> response = iVideoService.recogn(video,languageChoose);

        System.out.println("response.getBody()" + response.getBody());

        if (Objects.equals(JsonUtils.getData(response.getBody(),"code"), "0")){
            return ResponseMessage.success(JsonUtils.JsonStringToObject(response.getBody()));
        }else {
            return ResponseMessage.fail(JsonUtils.JsonStringToObject(response.getBody()));
        }

    }

    @GetMapping("/task_status/{taskId}")
    public ResponseMessage<User> taskStatus(@PathVariable String taskId) throws JsonProcessingException {
        ResponseEntity<String> response = iVideoService.taskStatus(taskId);

        if (Objects.equals(JsonUtils.getData(response.getBody(),"code"), "0")){
            return ResponseMessage.success(JsonUtils.JsonStringToObject(response.getBody()));
        }else {
            return ResponseMessage.fail(JsonUtils.JsonStringToObject(response.getBody()));
        }
    }

    @PostMapping("/trans_video")
    public ResponseMessage<User> trans_video(@RequestParam("video") MultipartFile video, @RequestParam("voice_role") String voice_role,@RequestParam("target_language") String target_language,@RequestParam("source_language") String source_language,@RequestParam("subtitle_type") String subtitle_type) throws JsonProcessingException {
        TransVideo transVideo = new TransVideo();

        transVideo.setTarget_language(target_language);
        transVideo.setSource_language(source_language);
        transVideo.setSubtitle_type(subtitle_type);
        transVideo.setVoice_role(voice_role);

        ResponseEntity<String> response = iVideoService.transVideo(video,transVideo);

        if (Objects.equals(JsonUtils.getData(response.getBody(),"code"), "0")){
            return ResponseMessage.success(JsonUtils.JsonStringToObject(response.getBody()));
        }else {
            return ResponseMessage.fail(JsonUtils.JsonStringToObject(response.getBody()));
        }
    }

    @GetMapping("/downloadDocument")//下载文档的原始文件
    public Object downloadDocument(@RequestParam("documentPath") String documentPath) throws IOException {
        ResponseEntity<byte[]> response = iVideoService.downloadDocument(documentPath);

        System.out.println(response);

        if (response.getStatusCode() == HttpStatus.OK){
            return response;
        } else {
            return ResponseMessage.fail("下载文件出错");
        }
    }


}
